//
//  HBGetRedManager.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/3.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBGetRedManager.h"
#import "HBGetUserRedResultParam.h"
#import "HBGetUserRedResultParser.h"

#import "HBGetShakeRedTimesParam.h"
#import "HBGetShakeRedTimesParser.h"
#import "HBGetShakeRedTimesInfo.h"

#import "HBGetInviteUserRedParam.h"
#import "HBGetInviteUserRedParser.h"


//#import "HBEnForceUserRedParam.h"
//#import "HBEnForceUserRedParser.h"

@implementation HBGetRedManager

static HBGetRedManager *sharedHBGetRedManager = nil;

+ (HBGetRedManager *)sharedHBGetRedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedHBGetRedManager = [[HBGetRedManager alloc]init];
        sharedHBGetRedManager.pageNum = 1;
    });
    return sharedHBGetRedManager;
}

+ (void)requestToGetShakeRedTimesWithResult:(void(^)(BOOL, NSString *, NSError *,int))result
{
    HBGetShakeRedTimesParam *param = [[HBGetShakeRedTimesParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    HBGetShakeRedTimesParser *parser = [[HBGetShakeRedTimesParser alloc]init];
    [NetCenter getDataFromURL:Method_getShakeRedTimes
                       params:param
                       parser:parser
                       result:^(ParserResult *myResult)
     {
         if (myResult.code ==0 )
         {
              HBGetShakeRedTimesInfo *info = (HBGetShakeRedTimesInfo *)myResult.result;
             
             if (result)
             {
                 result(YES, myResult.message, nil,[info.dataInfo intValue]);
             }
         }
         else
         {
             if (result)
             {
                 result(NO, myResult.message, nil,0);
             }
         }
         
     } failure:^(NSError *error)
     {
         if (result)
         {
             result(NO, @"", error,0);
         }
     }];
}

+ (void)requestToGetUserShakeRedWithResult:(void (^)(BOOL, NSString *, NSError *))result
{
    HBGetUserRedResultParam *param = [[HBGetUserRedResultParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    HBGetUserRedResultParser *parser = [[HBGetUserRedResultParser alloc]init];
    [NetCenter getDataFromURL:Method_getUserRedResult
                       params:param
                       parser:parser
                       result:^(ParserResult *myResult)
     {
         if (myResult.code ==0 )
         {
             HBGetUserRedResultInfo *info = (HBGetUserRedResultInfo *)myResult.result;
             
             [self sharedHBGetRedManager].userShakeRedInfo = info;
             if (result)
             {
                 result(YES, myResult.message, nil);
             }
         }
         else
         {
             if (result)
             {
                 result(NO, myResult.message, nil);
             }
         }
         
     } failure:^(NSError *error)
     {
         if (result)
         {
             result(NO, @"", error);
         }
     }];
}

+ (void)requestToGetInviteUserRedWithResult:(void (^)(BOOL, NSString *, NSError *,BOOL))result
{
    HBGetInviteUserRedParam *param = [[HBGetInviteUserRedParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.pageNum = [NSString stringWithFormat:@"%d",[self sharedHBGetRedManager].pageNum];
    HBGetInviteUserRedParser *parser = [[HBGetInviteUserRedParser alloc]init];
    [NetCenter getDataFromURL:Method_getInviteUserRed
                       params:param
                       parser:parser
                       result:^(ParserResult *myResult)
     {
         if (myResult.code ==0 )
         {
             HBGetInviteUserRedInfo *info = (HBGetInviteUserRedInfo *)myResult.result;
             if ([self sharedHBGetRedManager].pageNum==1)
             {
                 [self sharedHBGetRedManager].inviteUserRedInfo = info;
                 if (result)
                 {
                     result(YES, myResult.message, nil,info.dataAry.count>0?NO:YES);
                 }
                 
             }
             else
             {
                 if (info.dataAry.count>0)
                 {
                     NSMutableArray *array = [NSMutableArray arrayWithArray:[self sharedHBGetRedManager].inviteUserRedInfo.dataAry];
                     [array addObjectsFromArray:info.dataAry];
                     [self sharedHBGetRedManager].inviteUserRedInfo.dataAry = array;
                     if (result)
                     {
                         result(YES, myResult.message, nil,info.dataAry.count>0?NO:YES);
                     }

                 }
                 else
                 {
                     [self sharedHBGetRedManager].pageNum--;
                     if (result)
                     {
                         result(YES, myResult.message, nil,info.dataAry.count>0?NO:YES);
                     }
                 }
                 
             }
             
             
         }
         else
         {
             if (result)
             {
                 result(NO, myResult.message, nil,NO);
             }
         }
         
     } failure:^(NSError *error)
     {
         if (result)
         {
             result(NO, @"", error,NO);
         }
     }];
}


/*
+ (void)requestToGetUserEnforcedUserRedWithRedId:(NSString *)redId result:(void (^)(BOOL, NSString *, NSError *))result
{
    HBEnForceUserRedParam *param = [[HBEnForceUserRedParam alloc]init];
    param.redId = redId;
   
    HBEnForceUserRedParser *parser = [[HBEnForceUserRedParser alloc]init];
    [NetCenter postDataFromURL:Method_enforceUserRed
                       params:param
                       parser:parser
                       result:^(ParserResult *myResult)
     {
         if (myResult.code ==0 )
         {
            
             if (result)
             {
                 result(YES, myResult.message, nil);
             }
         }
         else
         {
             if (result)
             {
                 result(NO, myResult.message, nil);
             }
         }
         
     } failure:^(NSError *error)
     {
         if (result)
         {
             result(NO, @"", error);
         }
     }];
}
 */

@end
